package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"gitee.com/themoonstone/toOffer/third"
	"io/ioutil"
	"net/http"
	"os"
	"strings"
	"sync"
	"time"
)

func main() {
	//node := forth.ListNode{
	//	Next: &forth.ListNode{
	//		Next: &forth.ListNode{
	//			Next: nil,
	//			Val:  3,
	//		},
	//		Val: 2,
	//	},
	//	Val: 1,
	//}
	//res := forth.ReverseList(&node)
	//res := first.MyPow1(2.0, -2)
	//fmt.Println(res)
	fmt.Println(third.LongestValidParentheses(")()())"))
	return
	/*
		payDuration := []string{"year"}
		socialFlag := []int{0}
		initialCode := "WDJ12000"
		initialV := 92
		var s string
		var s1 string
		for _, duration := range payDuration {
			for _, social := range socialFlag {
				for i := 0; i <= 65; i++ {
					if i%5 == 0 {
						s += fmt.Sprintf("%s-%d-%d %s ", duration, social, i, fmt.Sprintf("%s%02d", initialCode, initialV+i/5-1))
						s1 += fmt.Sprintf("%s%02d;", initialCode, initialV+i/5-1)
					} else {
						s += fmt.Sprintf("%s-%d-%d %s ", duration, social, i, fmt.Sprintf("%s%02d", initialCode, initialV+i/5))
						s1 += fmt.Sprintf("%s%02d;", initialCode, initialV+i/5)
					}
				}
			}
		}

		socialFlag = []int{1}
		initialV = 79
		for _, duration := range payDuration {
			for _, social := range socialFlag {
				for i := 0; i <= 65; i++ {
					if i%5 == 0 {
						s += fmt.Sprintf("%s-%d-%d %s ", duration, social, i, fmt.Sprintf("%s%02d", initialCode, initialV+i/5-1))
						s1 += fmt.Sprintf("%s%02d;", initialCode, initialV+i/5-1)
					} else {
						s += fmt.Sprintf("%s-%d-%d %s ", duration, social, i, fmt.Sprintf("%s%02d", initialCode, initialV+i/5))
						s1 += fmt.Sprintf("%s%02d;", initialCode, initialV+i/5)
					}
				}
			}
		}
		f, _ := os.OpenFile("code-08-17-2.txt", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0777)
		f.Write([]byte(s))
		f.Write([]byte("\n"))
		fmt.Println(s)
	*/

}

func de() {
	var dd interface{}
	data := ``
	err := json.Unmarshal([]byte(data), &dd)
	if nil != err {
		panic(err)
	}
	fmt.Println(data)

}

//var nodes = &tree.TreeNode{
//	Val: -2,
//	//Left: &toOffer.TreeNode{
//	//	Val: 4,
//	//	Left: &toOffer.TreeNode{
//	//		Val: 11,
//	//		Right: &toOffer.TreeNode{
//	//			Val: 2,
//	//		},
//	//		Left: &toOffer.TreeNode{
//	//			Val: 7,
//	//		},
//	//	},
//	//},
//	Right: &tree.TreeNode{
//		Val: 3,
//		//Right: &toOffer.TreeNode{
//		//	Val: 4,
//		//	Right: &toOffer.TreeNode{
//		//		Val: 1,
//		//	},
//		//	Left: &toOffer.TreeNode{
//		//		Val: 5,
//		//	},
//		//},
//		//Left: &toOffer.TreeNode{
//		//	Val: 13,
//		//},
//	},
//}
type AutoGenerated struct {
	SkuName     string `json:"sku_name"`
	AppID       string `json:"app_id"`
	BuID        string `json:"bu_id"`
	Relation    int    `json:"relation"`
	InsuredInfo struct {
		Age    int `json:"age"`
		Gender int `json:"gender"`
		Social int `json:"social"`
	} `json:"insured_info"`
	HolderInfo struct {
		Age    int `json:"age"`
		Gender int `json:"gender"`
		Social int `json:"social"`
	} `json:"holder_info"`
	PayDuration string `json:"pay_duration"`
	Action      string `json:"action"`
}

type Resp struct {
	RetCode    string `json:"ret_code"`
	RetMsg     string `json:"ret_msg"`
	RetContent struct {
		RequestID    string `json:"request_id"`
		Code         string `json:"code"`
		Msg          string `json:"msg"`
		Action       string `json:"action"`
		AppID        string `json:"app_id"`
		BuID         string `json:"bu_id"`
		PolicyID     int64  `json:"policy_id"`
		UserID       int    `json:"user_id"`
		Period       int    `json:"period"`
		Amount       int    `json:"amount"`
		RenewAmount  int    `json:"renew_amount"`
		PaidAmount   int    `json:"paid_amount"`
		RefundAmount int    `json:"refund_amount"`
		Data         struct {
			RequestID string `json:"request_id"`
			PolicyURL string `json:"policy_url"`
			PolicyNo  string `json:"policy_no"`
		} `json:"data"`
		ApplicantCheckResult struct {
			CertIDCard string `json:"cert_id_card"`
			Result     string `json:"result"`
		} `json:"applicant_check_result"`
		InsuredCheckResults struct {
			CertIDCard string `json:"cert_id_card"`
			Result     string `json:"result"`
		} `json:"insured_check_results"`
		EbaoCardBalance int `json:"ebao_card_balance"`
		Response        struct {
			ReturnInfo struct {
				GeneralInfoReturn struct {
					ErrorCode     string `json:"ErrorCode"`
					ErrorMessage  string `json:"ErrorMessage"`
					PlateformCode string `json:"PlateformCode"`
					UUID          string `json:"UUID"`
				} `json:"GeneralInfoReturn"`
				PolicyInfoReturns struct {
					PolicyInfoReturn struct {
						DownloadURL    string `json:"DownloadUrl"`
						InsuredReturns struct {
							InsuredReturn struct {
								CheckMessage string `json:"CheckMessage"`
								CheckResult  string `json:"CheckResult"`
								InsuredSeqNo string `json:"InsuredSeqNo"`
							} `json:"InsuredReturn"`
						} `json:"InsuredReturns"`
						PolicyNo    string `json:"PolicyNo"`
						PolicyURL   string `json:"PolicyUrl"`
						SaveMessage string `json:"SaveMessage"`
						SaveResult  string `json:"SaveResult"`
						SaveTimes   string `json:"SaveTimes"`
						SerialNo    string `json:"SerialNo"`
					} `json:"PolicyInfoReturn"`
				} `json:"PolicyInfoReturns"`
			} `json:"ReturnInfo"`
		} `json:"response"`
	} `json:"ret_content"`
}

func PostRenbao() {
	data := AutoGenerated{
		SkuName:  "",
		AppID:    "ebao2696233202",
		BuID:     "ebao",
		Relation: 3,
		InsuredInfo: struct {
			Age    int `json:"age"`
			Gender int `json:"gender"`
			Social int `json:"social"`
		}{
			Gender: 1,
		},
		HolderInfo: struct {
			Age    int `json:"age"`
			Gender int `json:"gender"`
			Social int `json:"social"`
		}{
			Gender: 1,
			Social: 0,
			Age:    50,
		},
		Action: "insure",
	}
	var socialFlag []int = []int{0, 1}
	var skuNames []string = []string{"renbao-health-2021-1", "renbao-health-2021-2", "renbao-health-2021-1-year", "renbao-health-2021-2-year"}
	var wg sync.WaitGroup
	var ch chan Resp = make(chan Resp, 50)
	var errch chan Resp = make(chan Resp, 50)
	f, err := os.OpenFile("insure-result.txt", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0777)
	if nil != err {
		return
	}
	errorf, err := os.OpenFile("insure-error-result.txt", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0777)
	client := http.Client{}
	count := 0
	for i := 1; i <= 65; i++ {
		wg.Add(1)
		go func(age int) {
			defer wg.Done()
			for _, sku := range skuNames {
				for _, flag := range socialFlag {
					var res Resp
					data.SkuName = sku
					if strings.Contains(sku, "year") {
						data.PayDuration = "year"
					} else {
						data.PayDuration = "month"
					}
					data.InsuredInfo.Age = age
					data.InsuredInfo.Social = flag
					data.HolderInfo.Age = age + 20
					bd, err := json.Marshal(data)
					request, err := http.NewRequest(strings.ToUpper("POST"), "http://localhost:27029/ebao-supply-admin/mock/action/insure", bytes.NewBuffer(bd))
					if err != nil {
						continue
					}

					resp, err := client.Do(request)
					if err != nil {
						continue
					}
					defer resp.Body.Close()
					da, err := ioutil.ReadAll(resp.Body)
					if nil != err {
						continue
					}

					err = json.Unmarshal(da, &res)
					if nil != err {
						continue
					}
					if res.RetCode != "SP000000" {
						errch <- res
					} else {
						ch <- res
					}
					fmt.Println("count:", count, "data:", string(da), "resp:", res)
					count++

				}
			}
		}(i)

	}
	for {
		select {
		case retData := <-ch:
			d, _ := json.Marshal(retData)
			f.Write(d)
			f.Write([]byte("\n"))
		case errData := <-errch:
			d, _ := json.Marshal(errData)
			errorf.Write(d)
			errorf.Write([]byte("\n"))
		case <-time.After(30 * time.Minute):
			os.Exit(1)
		}
	}
	wg.Wait()
}
